import handler.MyInvocationHandler;
import org.junit.Test;
import service.SomeService;
import service.impl.SomeServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author HLF
 * @version V1.0
 * @package PACKAGE_NAME
 * @date 2021/10/14 20:34
 */
public class MyApp {
    public static void main(String[] args) {
        SomeService someService = new SomeServiceImpl();
        someService.doSome();
        System.out.println("==================");
        someService.doOther();
    }

    /**
     * 实现业务方法与非业务方法的分离，达到在原有方法不变的基础上实现功能增强
     */
    @Test
    public void test01(){
        //1.创建目标类(SomeServiceImpl)对象
        SomeService target = new SomeServiceImpl();
        //2.创建InvocationHandler对象(可以使用Spring的注解创建对象)
        InvocationHandler handler = new MyInvocationHandler(target);
        //3.使用Proxy类的静态方法Proxy.newProxyInstance()创建代理对象
        SomeService daili = (SomeService) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                handler);
        //4.通过代理对象执行目标方法
        daili.doSome();
        System.out.println("=================================================");
        daili.doOther();
    }
}
